﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Moonlite.UI.Shell.Controls {
    public class HyperlinkButton : Control {
        private bool autoSize = true;
        [Browsable(true)]
        public new bool AutoSize {
            get { return autoSize; }
            set { autoSize = value; }
        }

        private FontStyle normalFontStyle = FontStyle.Regular;
        public FontStyle NormalFontStyle {
            get { return normalFontStyle; }
            set { normalFontStyle = value; }
        }

        private FontStyle hoverFontStyle = FontStyle.Underline;
        public FontStyle HoverFontStyle {
            get { return hoverFontStyle; }
            set { hoverFontStyle = value; }
        }

        private FontStyle clickFontStyle = FontStyle.Underline;
        public FontStyle ClickFontStyle {
            get { return clickFontStyle; }
            set { clickFontStyle = value; }
        }

        private Color hoverColor = Color.DodgerBlue;
        public Color HoverColor {
            get { return hoverColor; }
            set { hoverColor = value; }
        }

        private Color clickColor = Color.CornflowerBlue;
        public Color ClickColor {
            get { return clickColor; }
            set { clickColor = value; }
        }

        [Browsable(true)]
        public new string Text {
            get { return base.Text; }
            set {
                base.Text = value;
                Invalidate();
            }
        }

        [Browsable(false)]
        public new Control Parent {
            get { return base.Parent; }
            set {
                base.Parent = value;
                if (value != null) {
                    value.MouseEnter += OnParentMouseEnter;
                }
            }
        }

        private bool IsMouseOver { get; set; }
        private bool IsMouseDown { get; set; }
        protected override void OnPaint(PaintEventArgs e) {
            Color color = IsMouseOver && !IsMouseDown ? HoverColor : IsMouseOver && IsMouseDown ? ClickColor : ForeColor;
            FontStyle fontStyle = IsMouseOver && !IsMouseDown ? HoverFontStyle : IsMouseOver && IsMouseDown ? ClickFontStyle : NormalFontStyle;
            SizeF size = e.Graphics.MeasureString(Text, Font);
            RectangleF bounds = new RectangleF((float)e.ClipRectangle.X, (float)e.ClipRectangle.Y, size.Width, size.Height);
            if (AutoSize == true) Bounds = new Rectangle(Bounds.X, Bounds.Y, (int)bounds.Width + 1, (int)bounds.Height + 1);
            using (SolidBrush brush = new SolidBrush(color)) {
                e.Graphics.DrawString(Text, new Font(Font, fontStyle), brush, bounds);
            }

            base.OnPaint(e);
        }

        protected override void OnMouseEnter(EventArgs e) {
            IsMouseOver = true;
            Invalidate();
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(EventArgs e) {
            IsMouseOver = false;
            Invalidate();
            base.OnMouseLeave(e);
        }

        protected override void OnMouseMove(MouseEventArgs e) {
            Invalidate();
            base.OnMouseMove(e);
        }

        protected override void OnMouseDown(MouseEventArgs e) {
            IsMouseDown = true;
            Invalidate();
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e) {
            IsMouseDown = false;
            Invalidate();
            base.OnMouseUp(e);
        }

        private void OnParentMouseEnter(object sender, EventArgs e) {
            IsMouseOver = false;
            Invalidate();
        }

        public HyperlinkButton() {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            ForeColor = Color.Blue;
            Font = new Font("Segoe UI", 10f);
            Cursor = Cursors.Hand;
        }
    }
}
